//
//  Extra2ViewController.m
//  ToucheTouche
//
//  Created by polytech polytech on 13/01/12.
//  Copyright (c) 2012 polytech. All rights reserved.
//

#import "Extra2ViewController.h"
#import "ModeGameViewController.h"
#import "GameAventureViewController.h"
#import "Level.h"

@implementation Extra2ViewController
@synthesize button11;
@synthesize button12;
@synthesize button13;
@synthesize button14;
@synthesize button15;
@synthesize button16;
@synthesize button17;
@synthesize button18;
@synthesize button21;
@synthesize button22;
@synthesize button23;
@synthesize button24;
@synthesize button25;
@synthesize button26;
@synthesize button27;
@synthesize button28;
@synthesize button31;
@synthesize button32;
@synthesize button33;
@synthesize button34;
@synthesize button35;
@synthesize button36;
@synthesize button37;
@synthesize button38;
@synthesize button41;
@synthesize button42;
@synthesize button43;
@synthesize button44;
@synthesize button45;
@synthesize button46;
@synthesize button47;
@synthesize button48;
@synthesize imageView11;
@synthesize imageView12;
@synthesize imageView13;
@synthesize imageView14;
@synthesize imageView15;
@synthesize imageView16;
@synthesize imageView17;
@synthesize imageView18;
@synthesize imageView21;
@synthesize imageView22;
@synthesize imageView23;
@synthesize imageView24;
@synthesize imageView25;
@synthesize imageView26;
@synthesize imageView27;
@synthesize imageView28;
@synthesize imageView31;
@synthesize imageView32;
@synthesize imageView33;
@synthesize imageView34;
@synthesize imageView35;
@synthesize imageView36;
@synthesize imageView37;
@synthesize imageView38;
@synthesize imageView41;
@synthesize imageView42;
@synthesize imageView43;
@synthesize imageView44;
@synthesize imageView45;
@synthesize imageView46;
@synthesize imageView47;
@synthesize imageView48;
@synthesize allButtons,allImagesView;
@synthesize labelScore;
@synthesize labelTemps;
@synthesize rand1,rand2, rand3, rand4, rand5 ,temps, score,nbImages;
@synthesize gameAventureViewController;
@synthesize delegate;
@synthesize alertLevel;

/* action lorsqu'on touche un bouton */
-(IBAction)actionImage: (id)sender{
    UIButton *buttonTouched = sender;
    NSInteger r = rand5;
    if([allButtons objectAtIndex:r]==buttonTouched){
        score+=50;
        nbImages+=1;
        [[Level sharedLevel] initExtraScore:score];
        labelScore.text = [NSString stringWithFormat:@"%.0f", [[Level sharedLevel] floatExtraScore]];
        [self stopTimerImage];
        [self timerImage];
            
    }
}

/* methode timer */
- (void)timer:(float) tmp{
    temps = tmp;
    chrono = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES] retain];
    // Affiche le compteur
    labelTemps.text = [NSString stringWithFormat:@"%.0f", temps];
}

/* methode affichant le chrono */
- (void) updateTimer: (NSTimer *) aTimer{
    --temps;
    // Affiche l'état du compte à rebour    
    labelTemps.text = [NSString stringWithFormat:@"%.0f", temps];    
    
    // Arrète le NSTimer lorsque le compte à rebour est à zéro
    if ( temps <= 0 ) {
        [self stopTimer];
    }
}


/* methode arret timer */
- (void)stopTimer {
    [chrono invalidate];
    chrono = nil;
    [timeImage invalidate];
    timeImage=nil;
    
    NSInteger life=0;
    if(nbImages>=7){
        if([[Level sharedLevel] floatLife] <=90){
            [[Level sharedLevel] addLife:10 :2];
            life=10;
        }else if([[Level sharedLevel] floatLife] <=95){
            [[Level sharedLevel] addLife:5 :1];
            life=5;
        }
    }
    else if(nbImages >=5){
        if([[Level sharedLevel] floatLife] <=95){
            [[Level sharedLevel] addLife:5 :1];
            life=5;
        }
    }
    // Affiche l'alerte avec le score
    alertLevel = [[UIAlertView alloc] initWithTitle:@"Résultat de l'extra" message:[NSString stringWithFormat:@"Score : %.0f \nVie gagnée : %d\nPassage au niveau 3", [[Level sharedLevel] floatExtraScore],life] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertLevel show];
    [alertLevel release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  {
    if (alertView == alertLevel) {
        if (buttonIndex == 0) {
            
            // ajout du score de l'extra au score global
            [self.gameAventureViewController addScoreNextExtra];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"AddScoreNextExtraNotification" object:nil];
            
            // mise a jour du nouveau niveau [bonnes valeurs]
            // init : score, score to add, score for extra, life, life to remove, lifeprogress, lifeprogress to remove, time level, time level to remove, current level, time image, background, image
            //[[Level sharedLevel] initLevel:[[Level sharedLevel] floatScore] :15 :750 :[[Level sharedLevel] floatLife] :5 :[[Level sharedLevel] floatLifeProgress] :0.05 :31 :1 :3 :0.7: @"backgroundLevel3": @"imageLevel3"];
            
            // POUR LES TESTS
            [[Level sharedLevel] initLevel:[[Level sharedLevel] floatScore] :10 :20 :[[Level sharedLevel] floatLife] :5 :[[Level sharedLevel] floatLifeProgress] :0.05 :10 :1 :3 :0.7: @"": @"imageTest.png"];
            
            
            
            NSLog(@"Score actuel : %.0f", [[Level sharedLevel] floatScore]);
            NSLog(@"Score a ajouter : %.0f", [[Level sharedLevel] floatScoreToAdd]);
            NSLog(@"vie actuelle : %.0f", [[Level sharedLevel] floatLife]);
            NSLog(@"vie a enlever : %.0f", [[Level sharedLevel] floatLifeToRemove]);
            NSLog(@"lifeprogress : %.0f", [[Level sharedLevel] floatLifeProgress]);
            NSLog(@"lifeprogress to remove : %.2f", [[Level sharedLevel] floatLifeProgressToRemove]);
            NSLog(@"temps du niveau : %d", [[Level sharedLevel] intTimeLevel]);
            NSLog(@"numero niveau : %d", [[Level sharedLevel] intCurrentLevel]);
            
            
            [self.gameAventureViewController initLabel]; // init des labels
            [[NSNotificationCenter defaultCenter] postNotificationName:@"InitLabelNotification" object:nil];
            [self.gameAventureViewController timerImage]; // deplacement image
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TimerNotification" object:nil];
            [self.gameAventureViewController timerLevel:[[Level sharedLevel] intTimeLevel]]; // temps du niveau
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TimerLevelNotification" object:nil];
            
            
            
            
            // Redirection vers le jeu
            [self setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
            [self dismissModalViewControllerAnimated:YES];
            
        }
    }
}



/* methode timer */
- (void)timerImage{

    timeImage = [[NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:@selector(updateTimerImage:) userInfo:nil repeats:YES] retain];
    [self changeImage];
}

/* methode affichant le chrono */
- (void) updateTimerImage: (NSTimer *) aTimer{
    [self changeImage];
}

/* methode arret timer */
- (void)stopTimerImage {
    [timeImage invalidate];
    timeImage = nil;
}

/* methode qui affiche l'image au hasard sur la grille */
- (void)changeImage {
    
    // boucle pour effacer l'ancienne image
    for (UIImageView *imageView in allImagesView) {
        imageView.image = nil;
    }
    rand1 = (arc4random() % 32);
    rand2 = (arc4random() % 32);
    while(rand2 == rand1){
          rand2 = (arc4random() % 32);
    }
    rand3 = (arc4random() % 32);
    while(rand3 == rand1 || rand3 == rand2 ){
        rand3 = (arc4random() % 32);
    }
    rand4 = (arc4random() % 32);
    while(rand4 == rand1 || rand4 == rand2 || rand4==rand3 ){
        rand4 = (arc4random() % 32);
    }
    rand5 = (arc4random() % 32);
    while(rand5 == rand1 || rand5 == rand2 || rand5==rand3 || rand5 == rand4){
        rand5 = (arc4random() % 32);
    }
    
    NSString *fileName = [NSString stringWithFormat: @"imageTest.png"];
    UIImageView *img = [allImagesView objectAtIndex:rand1];
    img.image = [UIImage imageNamed:fileName];
    img = [allImagesView objectAtIndex:rand2];
    img.image = [UIImage imageNamed:fileName];
    img = [allImagesView objectAtIndex:rand3];
    img.image = [UIImage imageNamed:fileName];
    img = [allImagesView objectAtIndex:rand4];
    img.image = [UIImage imageNamed:fileName];
    img = [allImagesView objectAtIndex:rand5];
    img.image = [UIImage imageNamed:[NSString stringWithFormat: @"imageTest2.png"]];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    temps=0;
    score=0;
    [[Level sharedLevel] initExtraScore:score];
    labelScore.text = [NSString stringWithFormat:@"%.0f", score];
    rand1=0;
    rand2=0;
    rand3=0;
    rand4=0;
    rand5=0;
    nbImages=0;
    labelScore.text = [NSString stringWithFormat:@"%d", score];
    allImagesView = [[NSArray alloc] initWithObjects:imageView11, imageView12,imageView13,imageView14,imageView15,imageView16,imageView17, imageView18, imageView21, imageView22, imageView23, imageView24, imageView25, imageView26, imageView27, imageView28, imageView31, imageView32, imageView33, imageView34, imageView35, imageView36, imageView37, imageView38, imageView41, imageView42, imageView43, imageView44, imageView45, imageView46, imageView47, imageView48,  nil];
    
    allButtons = [[NSArray alloc] initWithObjects:button11,button12,button13,button14,button15,button16, button17, button18, button21, button22, button23, button24, button25, button26, button27, button28, button31, button32, button33, button34, button35, button36, button37, button38, button41, button42, button43, button44, button45, button46, button47, button48, nil];
    for (UIImageView *imageView in allImagesView) {
        imageView.image = nil;
    }
    for (UIButton *buttons in allButtons) {
        buttons = nil;
    }
    [self timer:7];
    [self timerImage];

    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setButton11:nil];
    [self setButton12:nil];
    [self setButton13:nil];
    [self setButton14:nil];
    [self setButton15:nil];
    [self setButton16:nil];
    [self setButton18:nil];
    [self setButton17:nil];
    [self setButton21:nil];
    [self setButton22:nil];
    [self setButton23:nil];
    [self setButton24:nil];
    [self setButton25:nil];
    [self setButton26:nil];
    [self setButton27:nil];
    [self setButton28:nil];
    [self setButton31:nil];
    [self setButton32:nil];
    [self setButton33:nil];
    [self setButton34:nil];
    [self setButton35:nil];
    [self setButton36:nil];
    [self setButton37:nil];
    [self setButton38:nil];
    [self setButton41:nil];
    [self setButton42:nil];
    [self setButton43:nil];
    [self setButton44:nil];
    [self setButton45:nil];
    [self setButton46:nil];
    [self setButton47:nil];
    [self setButton48:nil];
    [self setImageView11:nil];
    [self setImageView12:nil];
    [self setImageView13:nil];
    [self setImageView14:nil];
    [self setImageView15:nil];
    [self setImageView16:nil];
    [self setImageView17:nil];
    [self setImageView18:nil];
    [self setImageView21:nil];
    [self setImageView22:nil];
    [self setImageView23:nil];
    [self setImageView24:nil];
    [self setImageView25:nil];
    [self setImageView26:nil];
    [self setImageView27:nil];
    [self setImageView28:nil];
    [self setImageView31:nil];
    [self setImageView32:nil];
    [self setImageView33:nil];
    [self setImageView34:nil];
    [self setImageView35:nil];
    [self setImageView36:nil];
    [self setImageView37:nil];
    [self setImageView38:nil];
    [self setImageView41:nil];
    [self setImageView42:nil];
    [self setImageView43:nil];
    [self setImageView44:nil];
    [self setImageView45:nil];
    [self setImageView46:nil];
    [self setImageView47:nil];
    [self setImageView48:nil];
    [self setLabelScore:nil];
    [self setLabelTemps:nil];
    self.temps=0;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
  return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);}

- (void)dealloc {
    [button11 release];
    [button12 release];
    [button13 release];
    [button14 release];
    [button15 release];
    [button16 release];
    [button18 release];
    [button17 release];
    [button21 release];
    [button22 release];
    [button23 release];
    [button24 release];
    [button25 release];
    [button26 release];
    [button27 release];
    [button28 release];
    [button31 release];
    [button32 release];
    [button33 release];
    [button34 release];
    [button35 release];
    [button36 release];
    [button37 release];
    [button38 release];
    [button41 release];
    [button42 release];
    [button43 release];
    [button44 release];
    [button45 release];
    [button46 release];
    [button47 release];
    [button48 release];
    [imageView11 release];
    [imageView12 release];
    [imageView13 release];
    [imageView14 release];
    [imageView15 release];
    [imageView16 release];
    [imageView17 release];
    [imageView18 release];
    [imageView21 release];
    [imageView22 release];
    [imageView23 release];
    [imageView24 release];
    [imageView25 release];
    [imageView26 release];
    [imageView27 release];
    [imageView28 release];
    [imageView31 release];
    [imageView32 release];
    [imageView33 release];
    [imageView34 release];
    [imageView35 release];
    [imageView36 release];
    [imageView37 release];
    [imageView38 release];
    [imageView41 release];
    [imageView42 release];
    [imageView43 release];
    [imageView44 release];
    [imageView45 release];
    [imageView46 release];
    [imageView47 release];
    [imageView48 release];
    [labelScore release];
    [labelTemps release];
    [allImagesView release];
    [allButtons release];
    [super dealloc];
}
@end
